<?php

use TpAmis\Renderers\Amis;
if (!function_exists('amis')) {
	/**
	 * @param $type
	 *
	 * @return \TpAmis\Renderers\Amis | \TpAmis\Renderers\Component
	 */
	function amis($type = null)
	{
		if (filled($type)) {
			return \TpAmis\Renderers\Component::make()->setType($type);
		}

		return \TpAmis\Renderers\Amis::make();
	}
}
if (!function_exists('filled')) {
	/**
	 * Determine if a value is "filled".
	 *
	 * @param  mixed  $value
	 * @return bool
	 */
	function filled($value)
	{
		return !blank($value);
	}
}

if (!function_exists('blank')) {
	/**
	 * Determine if the given value is "blank".
	 *
	 * @param  mixed  $value
	 * @return bool
	 */
	function blank($value)
	{
		if (is_null($value)) {
			return true;
		}

		if (is_string($value)) {
			return trim($value) === '';
		}

		if (is_numeric($value) || is_bool($value)) {
			return false;
		}

		if ($value instanceof Countable) {
			return count($value) === 0;
		}

		return empty($value);
	}
}
if (!function_exists('amisMake')) {
	function amisMake()
	{
		return Amis::make();
	}
}
if (!function_exists('aApi')) {
	/**
	 * @param string $method 支持 get、post、put、delete、patch
	 * @param string $url 地址
	 * @param array|object $data 提交数据
	 * @param array $options 其他属性参数
	 */
	function aApi($method = 'get', $url = '', $data = [], $options = [])
	{
		$Api = amisMake()->BaseApi()->method($method)->url($url);
		if (!empty($data)) {
			$Api->data($data);
		}
		if (!empty($options)) {
			foreach ($options as $name => $value) {
				$Api->$name($value);
			}
		}
		return $Api->toArray();
	}
}
if (!function_exists('aGet')) {
	/**
	 * @name amis的get请求
	 * @param string $url 地址
	 * @param array|object $data 提交数据
	 * @param array $options 其他属性参数
	 */
	function aGet($url = '', $data = [], $options = [])
	{
		return aApi('get', $url, $data, $options);
	}
}
if (!function_exists('aPost')) {
	/**
	 * @param string $url 地址
	 * @param array|object $data 提交数据
	 * @param array $options 其他属性参数
	 */
	function aPost($url = '', $data = [], $options = [])
	{
		return aApi('post', $url, $data, $options);
	}
}
if (!function_exists('getCurrentURL')) {

	function getCurrentURL()
	{
		$url = 'http';
		if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
			$url .= 's';
		}
		$url .= '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		$url .= $_SERVER['QUERY_STRING'];
		return $url;
	}
}

if (!function_exists('this_method')) {

	function this_method($this_method)
	{
		return explode('::', $this_method)[1];
	}
}
if (!function_exists('get_fun_args')) {

	function get_fun_args($className, $functionName, $args = [])
	{
		$reflection = new ReflectionMethod($className, $functionName);
		$parameters = $reflection->getParameters();
		$paramNames = [];
		foreach ($parameters as $key => $param) {
			$paramNames[$param->name] = isset($args[$key]) ? $args[$key] : null; // 使用参数名作为关联数组的键
		}
		return $paramNames;
	}
}


function DataToAmis($originalArray, $maps = ['data' => 'items', 'current_page' => 'page', 'perPage' => 'per_page', 'total' => 'total'])
{
	// 获取原始数组的所有键名
	$keys = array_keys($originalArray);

	// 获取原始数组的所有值
	$values = array_values($originalArray);

	foreach ($maps as $oldKey => $newKey) {
		$keyIndex = array_search($oldKey, $keys);

		// 更换键名
		$keys[$keyIndex] = $newKey;
	}
	// 查找要更换的键名在键名数组中的位置


	$newArray = array_combine($keys, $values);

	return $newArray;
}

function AmisJson($data, $msg = '', $status = 0)
{
	//如何保证当前代码之前无输出
	if (ob_get_length() > 0) {
		ob_clean();
	}

	dump(class_exists(\think\Db::getLastSql()));
	die;

	//设置header 输出json
	header('Content-Type:application/json; charset=utf-8');
	if (is_object($data)) {

		try {
			$data = $data->toArray();

		} catch (\Throwable $th) {
			return json([
				'message' => $th->getMessage(),
				'trace' => $th->getTraceAsString(),
			], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

		}
	}

	$data = DataToAmis($data);
	if (class_exists('\think\facade\Db')) {
		$sql = call_user_func('\think\facade\Db::getLastSql');
	}
	if (class_exists('\think\Db')) {
		$sql = call_user_func('\think\Db::getLastSql');
	}
	$data = [
		'data' => $data,
		'msg' => $msg,
		'status' => $status,
		'debug' => $sql
	];
	return json($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}


if (!function_exists('adminUrl')) {
	function adminUrl(string $url = '', array $vars = [], $suffix = true, $domain = false)
	{
		$urlquery = '';
		if (!empty($vars)) {
			$urlquery = '?' . getUrlQuery($vars);
		}
		return (string) url($url, [], $suffix, $domain) . $urlquery;
	}
}
